package com.cheeven.cwliu.citation.steps;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class StepBase {

	private static final Logger LOGGER = Logger.getLogger("Citation.Download.Steps");
	private static final String APP_CONFIG_PREFIX = "cwliu.citation";
	private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#,###");
	private static final XPath XPATH = XPathFactory.newInstance().newXPath();
	private static final HttpUtil http = new HttpUtil();
	private static final HtmlUtil html = new HtmlUtil();

	protected static void logInfo(String message) {
		LOGGER.log(Level.INFO, message);
		//System.out.println(message);
	}

	protected static void logInfo(String message, Object... params) {
		LOGGER.log(Level.INFO, message, params);
		/*
		for(int i=0; i<params.length; i++) {
			message = message.replace("{" + i + "}", "%s");
		}
		System.out.printf(message, params);
		System.out.println();
		*/ 
	}

	private static final String TAB = "\t"; 
	protected static void logInfo(int indent, String message) {
		for(int i=0; i<indent; i++) {
			message = TAB + message;
		}
		LOGGER.log(Level.INFO, message);
		//System.out.println(message);
	}

	protected static void logInfo(int indent, String message, Object... params) {
		for(int i=0; i<indent; i++) {
			message = TAB + message;
		}
		LOGGER.log(Level.INFO, message, params);
		/*
		for(int i=0; i<params.length; i++) {
			message = message.replace("{" + i + "}", "%s");
		}
		System.out.printf(message, params);
		System.out.println();
		*/ 
	}

	protected static void logError(String msg, Throwable thrown) {
		LOGGER.log(Level.SEVERE, msg, thrown);
	}

	protected static void logError(Throwable thrown) {
		logError(null, thrown);
	}

	protected static String getAppPrefix() {
		return APP_CONFIG_PREFIX;
	}

	protected static DecimalFormat getDecimalFormat() {
		return DECIMAL_FORMAT;
	}

	protected static Integer parseInteger(String source) {
		try {
			Number number = DECIMAL_FORMAT.parse(source);
			return number.intValue();
		} catch (ParseException ex) {
			logError(ex);
			return null;
		}
	}

	protected static Double parseDouble(String source) {
		try {
			Number number = DECIMAL_FORMAT.parse(source);
			return number.doubleValue();
		} catch (ParseException ex) {
			logError(ex);
			return null;
		}
	}

	protected static String cleanNumberFormat(String source) {
		try {
			Number number = DECIMAL_FORMAT.parse(source);
			return String.valueOf(number);
		} catch (ParseException ex) {
			logError(ex);
			return null;
		}
	}

	protected static XPath getXPath() {
		return XPATH;
	}

	protected static HtmlUtil getHtmlUtil() {
		return html;
	}

	protected static HttpUtil getHttpUtil() {
		return http;
	}

	protected static String httpGet(String url) {
		return getHttpUtil().get(url);
	}

	protected static String httpPost(String url, String[][] params) {
		return getHttpUtil().post(url, params);
	}

	protected static Document toDOM(String source) {
		return html.parseDOMDocument(source);
	}

	protected static String xpath(String pattern, Node node) {
		try {
			return getXPath().evaluate(pattern, node);
		} catch (XPathExpressionException ex) {
			logError(ex);
			return null;
		}
	}

	protected static Object xpath(String pattern, Node node, QName qname) {
		try {
			return getXPath().evaluate(pattern, node, qname);
		} catch (XPathExpressionException ex) {
			logError(ex);
			return null;
		}
	}

	protected static File saveFile(String folderName, String journalName, String fileName, String content) {
		FileWriter out = null;
		try {
			File folder = new File(folderName);
			if (!folder.exists()) {
				folder.mkdirs();
			}
			folder = new File(folder, journalName);
			if (!folder.exists()) {
				folder.mkdirs();
			}
			File outputFile = new File(folder, fileName);
			out = new FileWriter(outputFile);
			out.write(content);
			out.close();
			
			return outputFile;

		} catch (IOException ex) {
			logError(ex);
		} finally {
			try {
				out.close();
			} catch (IOException ex) {
				logError(ex);
			}
		}
		
		return null;

	}
}
